package com.cn.lyl.servlet.news;

import com.alibaba.fastjson.JSONObject;
import com.cn.lyl.model.NewsModel;
import com.cn.lyl.model.Page;
import com.cn.lyl.service.NewsService;
import com.cn.lyl.service.impl.NewsServiceImpl;
import com.cn.lyl.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/news/list")
public class FindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String title = req.getParameter("titles");
        String type = req.getParameter("type");
        String isHome = req.getParameter("isHome");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        NewsModel newsModel = new NewsModel();
        newsModel.setDependenceObj(type);
        newsModel.setTitle(title);
        newsModel.setStart(start);
        newsModel.setEnd(end);
        Page page=new Page();
        page.setPageCurr(Tool.curr(req.getParameter("page")));
        page.setPageSize(Tool.size(req.getParameter("limit")));
        newsModel.setPage(page);
        newsModel.setIsHome(isHome);
        NewsService newsService = new NewsServiceImpl();

        List<NewsModel> allNews = newsService.findAllNews(newsModel);
        newsService.findAllNews(newsModel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","");
        jsonObject.put("count",newsService.getCount(newsModel));
        jsonObject.put("data",allNews);
        resp.getWriter().print(jsonObject);


    }
}
